package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.BsPurview;
import com.drcoffee.drcoffeemall.entry.BsPurviewType;
import com.drcoffee.drcoffeemall.entry.BsUser;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.service.BsUserService;
import com.drcoffee.drcoffeemall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.GeneralSecurityException;
import java.util.Map;

/**
 * 后台用户
 *
 * @author ShiHao Zhou
 */
@RequestMapping("/bs_user")
@RestController
public class BsUserController {
    @Autowired
    BsUserService bsUserService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /*
     * 登录验证
     * */
    @PostMapping("/login")
    public Result login(@RequestBody BsUser bsUser) throws GeneralSecurityException {
        return bsUserService.login(bsUser);
    }

    /*
     * 删除Token
     * */
    @PostMapping("/removeToken")
    public boolean removeToken(@RequestHeader String Token) {
        return bsUserService.removeToken(Token);
    }

    /*
     * 更新Token
     * */
    @PostMapping("/renewalToken")
    public void renewalToken(@RequestHeader String Token) {
        bsUserService.renewalToken(Token);
    }

    /*
     * 验证Token
     * */
    @GetMapping("/verifyToken")
    public boolean verifyToken(@RequestHeader String Token) {
        return redisTemplate.opsForValue().get(Token) != null;
    }

    /*
     * 根据账号查询用户
     * */
    @GetMapping("/getBsUserByAccount")
    public Result getBsUserByAccount(@RequestParam String account) {
        return bsUserService.getBsUserByAccount(account);
    }

    /*
     * 查询用户
     * */
    @GetMapping("/getBsUser")
    public Result getBsUser(@RequestParam String search) {
        return bsUserService.getBsUser(search);
    }

    /*
     * 添加用户
     * */
    @PostMapping("addBsUser")
    public Result addBsUser(@RequestBody BsUser bsUser) {
        return bsUserService.addBsUser(bsUser);
    }

    /*
     * 修改用户
     * */
    @PostMapping("editBsUser")
    public Result editBsUser(@RequestBody BsUser bsUser) {
        return bsUserService.editBsUser(bsUser);
    }


    /*
     * 删除用户
     * */
    @GetMapping("deleteBsUser")
    public Result deleteBsUser(@RequestParam Integer id) {
        return bsUserService.deleteBsUser(id);
    }

    /*
    * 设置用户角色绑定
    * */
    @PostMapping("/setBsUserRole")
    public Result setBsUserRole(@RequestBody Map<String, Object> bs_user_role) {
        return bsUserService.setBsUserRole(bs_user_role);
    }



    /*
     * 查询角色权限
     * */
    @GetMapping("/getBsPurviewByRole")
    public Result getBsPurviewByRole(@RequestParam Integer role_id) {
        return bsUserService.getBsPurviewByRole(role_id);
    }

    /*
     * 查询用户角色
     * */
    @GetMapping("/getBsRoleByUser")
    public Result getBsRoleByUser(@RequestParam Integer id) {
        return bsUserService.getBsRoleByUser(id);
    }

    /*
     * 查询所有角色
     * */
    @GetMapping("/getAllBsRole")
    public Result getAllBsRole() {
        return bsUserService.getAllBsRole();
    }

    /*
     * 查询所有权限
     * */
    @GetMapping("/getAllBsPurview")
    public Result getAllBsPurview() {
        return bsUserService.getAllBsPurview();
    }

    /*
     * 修改 & 添加角色
     * */
    @PostMapping("/setPurviewByRole")
    public Result setPurviewByRole(@RequestBody Map<String, Object> role) {
        return bsUserService.setPurviewByRole(role);
    }

    /*
     * 删除角色
     * */
    @GetMapping("/deleteRole")
    public Result deleteRole(@RequestParam Integer role_id) {
        return bsUserService.deleteRole(role_id);
    }


    /*
     * 添加权限
     * */
    @PostMapping("addBsPurview")
    public Result addBsPurview(@RequestBody BsPurview bsPurview) {
        return bsUserService.addBsPurview(bsPurview);
    }

    /*
     * 修改权限
     * */
    @PostMapping("editBsPurview")
    public Result editBsPurview(@RequestBody BsPurview bsPurview) {
        return bsUserService.editBsPurview(bsPurview);
    }

    /*
     * 删除权限
     * */
    @GetMapping("deleteBsPurview")
    public Result editBsPurview(@RequestParam Integer id) {
        return bsUserService.deleteBsPurview(id);
    }


    /*
     * 添加权限组
     * */
    @PostMapping("addBsPurviewType")
    public Result addBsPurviewType(@RequestBody BsPurviewType bsPurviewType) {
        return bsUserService.addBsPurviewType(bsPurviewType);
    }

    /*
     * 修改权限组
     * */
    @PostMapping("editBsPurviewType")
    public Result editBsPurviewType(@RequestBody BsPurviewType bsPurviewType) {
        return bsUserService.editBsPurviewType(bsPurviewType);
    }

    /*
     * 删除权限组
     * */
    @GetMapping("deleteBsPurviewType")
    public Result deleteBsPurviewType(@RequestParam Integer id) {
        return bsUserService.deleteBsPurviewType(id);
    }

}
